#include "dataconvert.h"

DataConvert::DataConvert() {}
/**
 * 按行主序转换二维数组，保持MATLAB原生数据顺序
 * @param arr 输入的二维数组（rows x cols）
 * @return QVector嵌套结构，外层为行，内层为列数据
 */
QVector<QVector<double>> DataConvert::convertRowMajor(const coder::array<double, 2U>& arr) {
    QVector<QVector<double>> result;
    const int rows = static_cast<int>(arr.size(0));
    const int cols = static_cast<int>(arr.size(1));

    result.reserve(rows);
    for (int i = 0; i < rows; ++i) {
        QVector<double> row(cols);
        std::copy(arr.data()  + i * cols,
                  arr.data()  + (i + 1) * cols,
                  row.begin());
        result.append(row);
    }
    return result;
}
QVector<QVector<double>> DataConvert::convertColsMajor(const coder::array<double, 2U>& arr) {
    QVector<QVector<double>> result;
    const int rows = static_cast<int>(arr.size(0));
    const int cols = static_cast<int>(arr.size(1));

    result.reserve(cols);
    for (int i = 0; i < cols; ++i) {
        QVector<double> row(rows);
        std::copy(arr.data()  + i * rows,
                  arr.data()  + (i + 1) * rows,
                  row.begin());
        result.append(row);
    }
    return result;
}
QVector<double> DataConvert::convertToQVector(const coder::array<double, 1U>& arr) {
    QVector<double> result;
    const int rows=arr.size(0);
    result.reserve(rows);   // 预分配内存提升性能

    for(int i = 0; i < rows;  ++i) {
        result.append(arr[i]);   // 逐元素拷贝
    }
    return result;
}
